<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javaScript完成全选 全取消 反选</title>
<script type="text/javascript">
	window.onload = function(){
		var OselectAll = document.getElementsByTagName("input")[0];
		var Ocancel = document.getElementsByTagName("input")[1];
		var Oreverse = document.getElementsByTagName("input")[2];
		OselectAll.onclick= selectAll;
		Ocancel.onclick = cancel;
		Oreverse.onclick = reverse;
		
	}
	function selectAll(){
		var Odiv2 = document.getElementById("div2");
		var Oinputs = Odiv2.childNodes;
		for(var i=0;i<Oinputs.length;i++){
			if(Oinputs[i].type=="checkbox"){
				Oinputs[i].checked=true;
			}	
		}
	}
	function cancel(){
		var Odiv2 = document.getElementById("div2");
		var Oinputs = Odiv2.childNodes;
		for(var i=0;i<Oinputs.length;i++){
			Oinputs[i].checked=false;
		}
	}
	function reverse(){
		var Odiv2 = document.getElementById("div2");
		var Oinputs = Odiv2.childNodes;
		for(var i=0;i<Oinputs.length;i++){
			if(Oinputs[i].type=="checkbox"){
				Oinputs[i].checked?(Oinputs[i].checked=false):(Oinputs[i].checked=true);
			}
		}
	}
</script>
</head>
<body>
<div id="div1">
	<input type="button" value="全选">
	<input type="button" value="全取消">
	<input type="button" value="反选">
</div>
<div id="div2">
		请选择：
		<input type="checkbox" name="hobby" value="study" checked>学习
		<input type="checkbox" name="hobby" value="swiming">游泳
		<input type="checkbox" name="hobby" value="running">跑步
		<input type="checkbox" name="hobby" value="music">听音乐
		<input type="checkbox" name="hobby" value="game">玩游戏
</div>
</body>
</html>